package com.szewec.ps.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.base.BaseApplication;
import com.szewec.ps.base.BaseUrl;
import com.szewec.ps.ui.login.LoginActivity;
import com.szewec.ps.util.okhttp.OkHttp;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class NetWorkUtil {
    private static Activity context;
    static String TAG = "NetWorkUtil";
    private static NetWorkUtil netWorkUtil = new NetWorkUtil();

    private NetWorkUtil() {

    }

    public static NetWorkUtil getInstance(Activity context) {
        NetWorkUtil.context = context;
        return netWorkUtil;
    }


    /**
     * 登录接口,获取token用于更新token
     */
    public static void updateToken(Request request, OkHttp.MyCallback callBack) {
        String username = (String) SharedPreferencesUtil.getData(PreferincesContext.USERNAME, "");
        String password = (String) SharedPreferencesUtil.getData(PreferincesContext.PASSWORD, "");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            Context context = BaseApplication.getContext();
            Intent intent = new Intent(context, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferincesContext.USERNAME, username);
        map.put(PreferincesContext.PASSWORD, password);
        OkHttp.postByForm(BaseUrl.platform + "/account/appLogin?", map, new OkHttp.MyCallback() {
            @Override
            public void onSuccess(String result) {
                LogUtil.i(TAG,"登陆结果:"+result);
                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject.get("code").equals("SUCCESS")){//登陆成功
                    String token = (String) jsonObject.get("result");
                    //存储token
                    SharedPreferencesUtil.putData(PreferincesContext.TOKEN,token);
                    Request newRequest;
                    LogUtil.i(TAG,"请求方法:"+request.method());
                    if (request.method().equals("GET")){
                        newRequest = new Request.Builder().url(request.url())
                                .addHeader("x-project-id",request.header("x-project-id"))
                                .addHeader("authorization",token)
                                .get()
                                .build();//采用get方式提交
                    }else {
                        newRequest = new Request.Builder().url(request.url())
                                .addHeader("x-project-id",request.header("x-project-id"))
                                .addHeader("authorization",token)
                                .post(request.body())
                                .build();//采用get方式提交
                    }

                    OkHttp.getClient().newCall(newRequest).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            OkHttp.handleCallBack(newRequest,response,callBack);
                        }
                    });
                }
            }
        });
    }

}
